﻿Imports ADOEntity
Imports System.Data

Partial Class Administrator_menuRight
    Inherits Web.UI.BasePage
    Private Const strMenuCode As String = "M63"
    Dim sMenuRightEntity As New userEntities
    Dim dsUser As New DataSet
    Dim clsQuery As clsDatabase
    Dim clsDataTable As New clsLINQToDataTable
    Dim clsSessionMenuRight As clsSessionMenuRight
    Dim strUserRight As String

#Region "Even Button"
    'Protected Sub btnShow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnShow.Click
    '    ' Dim userEntitys As New userEntities

    '    IsDataBind()

    '    '        Dim qryShow = From userRight In userEntitys.SUSERRIGHT Group Join mr In (From MenuUserRight In userEntitys.SMENURIGHT Where MenuUserRight.PROGRAMCODE = ddlUserRight.SelectedValue) On userRight.USERRIGHT Equals mr.USERRIGHT _
    '    '                      Into both = Group _
    '    '                                 From b In both.DefaultIfEmpty _
    '    'Select userRight.USERRIGHT, userRight.USERRIGHTDESC, b.MQUERY _
    '    '                      , b.MADD, b.MMODIFY, b.MDELETE, b.MPRINT

    '    '        grdRight.DataSource = qryShow
    '    '        grdRight.DataBind()


    'End Sub
#End Region

#Region "Even GridView"

   
    Protected Sub gvMenuRight_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvMenuRight.RowEditing
        gvMenuRight.EditIndex = e.NewEditIndex
        bindTogrid()

    End Sub

    Protected Sub gvMenuRight_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gvMenuRight.RowCancelingEdit
        gvMenuRight.EditIndex = -1
        bindTogrid()
    End Sub

    Protected Sub gvMenuRight_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles gvMenuRight.RowUpdated

    End Sub

    'Protected Sub gvMenuRight_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvMenuRight.RowUpdating
    '    Dim strProgramCode As String = ddlUSerRight.SelectedValue
    '    Dim strUSERRIGHT As String = ""

    '    Dim MQUERY As Integer
    '    Dim chkQuery As CheckBox = gvMenuRight.Rows(e.RowIndex).Cells(3).FindControl("chkQuery")
    '    'Dim chkadd As CheckBox = gvMenuRight.Rows(e.RowIndex).Cells(4).FindControl("chkAdd")
    '    'Dim chkModify As CheckBox = gvMenuRight.Rows(e.RowIndex).Cells(5).FindControl("chkModify")
    '    'Dim chkDelete As CheckBox = gvMenuRight.Rows(e.RowIndex).Cells(6).FindControl("chkDelete")
    '    'Dim chkPrint As CheckBox = gvMenuRight.Rows(e.RowIndex).Cells(7).FindControl("chkPrint")

    '    MQUERY = 0
    '    'MADD = 0
    '    'MMODIFY = 0
    '    'MDELETE = 0
    '    'MPRINT = 0
    '    strUSERRIGHT = gvMenuRight.Rows(e.RowIndex).Cells(1).Text

    '    If chkQuery.Checked = True Then MQUERY = 1
    '    'If chkadd.Checked = True Then MADD = 1
    '    'If chkModify.Checked = True Then MMODIFY = 1
    '    'If chkDelete.Checked = True Then MDELETE = 1
    '    'If chkPrint.Checked = True Then MPRINT = 1

    '    If saveData(strProgramCode, strUSERRIGHT, , MQUERY) = True Then
    '        gvMenuRight.EditIndex = -1
    '        Call btnShow_Click(sender, e)
    '    Else
    '        ' Response.Write("<script language=javascript>if(window.alert('พบข้อผิดพลาด ไม่สามารถบันทึกได้'),true)</script>")
    '        Exit Sub
    '    End If

    'End Sub
#End Region

#Region "even DropdownList"
    Protected Sub ddlProgram_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlUSerRight.SelectedIndexChanged
        If (ddlUSerRight.SelectedValue = "0") Then
            setButton(1)
        Else
            setButton(3)
        End If
        IsDataBind()
    End Sub

#End Region

#Region "Even Page "


    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        btnShow.Text = Resources.resButton.btnShow
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            chkIsLogin()
            clsSessionMenuRight = Session("ClsMenuRight")
            strUserRight = Request.QueryString("userRight")
            If Not (IsPostBack) Then
                If (strUserRight = clsSessionMenuRight.getUserRight) Then
                    If (clsSessionMenuRight.getRoleGroup > 79) Then
                        enableControl(True)
                    Else
                        enableControl(False)
                    End If

                Else
                    enableControl(True)
                End If
                CreateControlMP()
                setButton(3)
                IsDataBind()
            Else
                dsUser = Session("dsTblMaster")
                ' bindTogrid()

            End If
        Catch ex As Exception

        End Try


    End Sub

    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        Session("dsTblMaster") = dsUser
    End Sub

#End Region

    Private Sub enableControl(ByVal chk As Boolean)
        btnEditMenuRight.Enabled = chk
        gvMenuRight.Enabled = chk

    End Sub

#Region "SetForm"

    Sub CreateControlMP()

        Dim lblForm As Label = CType(Master.FindControl("lblForm"), Label)
        lblForm.Text = "สิทธิ์การใช้งาน"
    End Sub

#End Region

#Region " Function "

    Private Sub setUserRightButton()
        Dim col As New Collection

        col.Add(btnSaveEdit, statusbtn.Modify.ToString)
        col.Add(btnEditMenuRight, statusbtn.Modify1.ToString)
        col.Add(btnPrint, statusbtn.print.ToString)

        setBtnToUserRight(col, strMenuCode, clsSessionMenuRight.getDsMenuRight)

    End Sub


    Private Sub loadDataToDropDown()
        Try
            Dim clsMasterDropDown As clsMasterDropDown = New clsMasterDropDown

            clsMasterDropDown.setDeptToDdlSearch(ddlDepartment, _
                                              strDeptCode:=clsSessionMenuRight.getDeptCode, _
                                              strRoleCode:=clsSessionMenuRight.getRoleGroup)

            clsMasterDropDown.setUserRightToDdlSearch(ddlUSerRight, clsSessionMenuRight.getRoleGroup, strDepCode:=ddlDepartment.SelectedValue)

            clsMasterDropDown = Nothing
        Catch ex As Exception
            WriteError(ex, "loadDataToDropDown", "menuRight")
        End Try
    End Sub

    Private Sub IsDataBind()
        Try
            clsSessionMenuRight = Session("ClsMenuRight")
            If dsUser.Tables.Contains("sMenuRight") Then
                dsUser.Tables.Clear()
            End If


            If (clsSessionMenuRight.getRoleGroup > 79) Then
                Dim qrymUserRight = From tblM In (From ur In sMenuRightEntity.SUSERRIGHT, pg In sMenuRightEntity.SPROGRAM
                                               Where ur.USERRIGHT = strUserRight
                                               Select ur.USERRIGHT, ur.USERRIGHTDESC, pg.PROGRAMCODE, pg.PROGRAMNAME,
                                                       pro_MQUERY = pg.MQUERY, pro_MADD = pg.MADD, pro_MMODIFY = pg.MMODIFY,
                                                       pro_MDELETE = pg.MDELETE, pro_MPRINT = pg.MPRINT)
                               Group Join mr In sMenuRightEntity.SMENURIGHT
                                   On mr.PROGRAMCODE Equals tblM.PROGRAMCODE And
                                       mr.USERRIGHT Equals tblM.USERRIGHT Into LeftJoinMR = Group _
                               From joinMR In LeftJoinMR.DefaultIfEmpty
                               Order By tblM.PROGRAMCODE Ascending
                               Select tblM.USERRIGHT, tblM.PROGRAMCODE, tblM.PROGRAMNAME,
                                       ISSHOW = If(joinMR.ISSHOW Is Nothing, False, If(joinMR.ISSHOW = True, True, False)),
                                       MQUERY = If(joinMR.MQUERY Is Nothing, False, If(joinMR.MQUERY = True, True, False)),
                                       MADD = If(joinMR.MADD Is Nothing, False, If(joinMR.MADD = True, True, False)),
                                       MMODIFY = If(joinMR.MMODIFY Is Nothing, False, If(joinMR.MMODIFY = True, True, False)),
                                       MDELETE = If(joinMR.MDELETE Is Nothing, False, If(joinMR.MDELETE = True, True, False)),
                                       MPRINT = If(joinMR.MPRINT Is Nothing, False, If(joinMR.MPRINT = True, True, False)),
                                       pro_MQUERY = If(tblM.pro_MQUERY Is Nothing, False, If(tblM.pro_MQUERY = True, True, False)),
                                       pro_MADD = If(tblM.pro_MADD Is Nothing, False, If(tblM.pro_MADD = True, True, False)),
                                       pro_MMODIFY = If(tblM.pro_MMODIFY Is Nothing, False, If(tblM.pro_MMODIFY = True, True, False)),
                                       pro_MDELETE = If(tblM.pro_MDELETE Is Nothing, False, If(tblM.pro_MDELETE = True, True, False)),
                                       pro_MPRINT = If(tblM.pro_MPRINT Is Nothing, False, If(tblM.pro_MPRINT = True, True, False))

                dsUser.Tables.Add(clsDataTable.LINQToDataTable(qrymUserRight, "sMenuRight").Copy)

            Else
                Dim qrymUserRight = From tblM In (From tblUserright In (From ur In sMenuRightEntity.SUSERRIGHT,
                                                                            pg In sMenuRightEntity.SPROGRAM
                                                                        Where ur.USERRIGHT = strUserRight
                                                                        Select ur.USERRIGHT, ur.USERRIGHTDESC, pg.PROGRAMCODE, pg.PROGRAMNAME,
                                                                                pro_MQUERY = pg.MQUERY, pro_MADD = pg.MADD, pro_MMODIFY = pg.MMODIFY,
                                                                                pro_MDELETE = pg.MDELETE, pro_MPRINT = pg.MPRINT) Join mn In
                                                                            sMenuRightEntity.SMENURIGHT On tblUserright.PROGRAMCODE Equals mn.PROGRAMCODE
                                                    Where mn.USERRIGHT = clsSessionMenuRight.getUserRight And mn.isShow = True
                                                    Select tblUserright.USERRIGHT, tblUserright.USERRIGHTDESC,
                                                    tblUserright.PROGRAMCODE, tblUserright.PROGRAMNAME, pro_mQuery = mn.MQUERY,
                                                    pro_mAdd = mn.MADD, pro_mModify = mn.MMODIFY, pro_mDelete = mn.MDELETE, pro_mPrint = mn.MPrint
                                                  ) Group Join mr In sMenuRightEntity.SMENURIGHT
                                   On mr.PROGRAMCODE Equals tblM.PROGRAMCODE And mr.USERRIGHT Equals tblM.USERRIGHT Into LeftJoinMR = Group _
                               From joinMR In LeftJoinMR.DefaultIfEmpty
                               Order By tblM.PROGRAMCODE Ascending
                                Select tblM.USERRIGHT, tblM.PROGRAMCODE, tblM.PROGRAMNAME,
                                       ISSHOW = If(joinMR.ISSHOW Is Nothing, False, If(joinMR.ISSHOW = True, True, False)),
                                       MQUERY = If(joinMR.MQUERY Is Nothing, False, If(joinMR.MQUERY = True, True, False)),
                                       MADD = If(joinMR.MADD Is Nothing, False, If(joinMR.MADD = True, True, False)),
                                       MMODIFY = If(joinMR.MMODIFY Is Nothing, False, If(joinMR.MMODIFY = True, True, False)),
                                       MDELETE = If(joinMR.MDELETE Is Nothing, False, If(joinMR.MDELETE = True, True, False)),
                                       MPRINT = If(joinMR.MPRINT Is Nothing, False, If(joinMR.MPRINT = True, True, False)),
                                       pro_MQUERY = If(tblM.pro_mQuery Is Nothing, False, If(tblM.pro_mQuery = True, True, False)),
                                       pro_MADD = If(tblM.pro_mAdd Is Nothing, False, If(tblM.pro_mAdd = True, True, False)),
                                       pro_MMODIFY = If(tblM.pro_mModify Is Nothing, False, If(tblM.pro_mModify = True, True, False)),
                                       pro_MDELETE = If(tblM.pro_mDelete Is Nothing, False, If(tblM.pro_mDelete = True, True, False)),
                                       pro_MPRINT = If(tblM.pro_mPrint Is Nothing, False, If(tblM.pro_mPrint = True, True, False))


                dsUser.Tables.Add(clsDataTable.LINQToDataTable(qrymUserRight, "sMenuRight").Copy)

            End If

            bindTogrid()

            clsQuery = Nothing

        Catch ex As Exception
            WriteError(ex, "IsDataBind", "menuRight")
        End Try

    End Sub

    Private Sub bindTogrid()
        gvMenuRight.DataSource = dsUser.Tables("sMenuRight")
        gvMenuRight.DataBind()

    End Sub
    Dim tblMenuRight As SMENURIGHT
    Dim icount As Integer = 0
    Function saveData(ByVal PROGRAMCODE As String, ByVal USERRIGHT As String, ByVal isShow As Boolean, ByVal MQUERY As Boolean, _
                      ByVal MAdd As Boolean, ByVal MModify As Boolean, ByVal MDelete As Boolean, ByVal MPrint As Boolean) As Boolean

        Dim result As Boolean = False
        icount = 0
        Try
            Dim qryMenuRight = (From menuRight In sMenuRightEntity.SMENURIGHT Where menuRight.PROGRAMCODE = PROGRAMCODE And menuRight.USERRIGHT = USERRIGHT).ToList

            If (isShow = True Or MQUERY = True Or MAdd = True Or MModify = True Or MDelete = True Or MPrint = True) Then
                icount = 1
            End If

            If (qryMenuRight.Count = 0) Then
                If (icount = 1) Then
                    tblMenuRight = New SMENURIGHT
                    tblMenuRight.USERRIGHT = USERRIGHT
                    tblMenuRight.PROGRAMCODE = PROGRAMCODE
                    tblMenuRight.ISSHOW = isShow
                    tblMenuRight.MQUERY = MQUERY
                    tblMenuRight.MADD = MAdd
                    tblMenuRight.MMODIFY = MModify
                    tblMenuRight.MDELETE = MDelete
                    tblMenuRight.MPRINT = MPrint
                    sMenuRightEntity.AddToSMENURIGHT(tblMenuRight)
                End If



            Else

                qryMenuRight(0).ISSHOW = isShow
                qryMenuRight(0).MQUERY = MQUERY
                qryMenuRight(0).MADD = MAdd
                qryMenuRight(0).MMODIFY = MModify
                qryMenuRight(0).MDELETE = MDelete
                qryMenuRight(0).MPRINT = MPrint
                sMenuRightEntity.DeleteObject(qryMenuRight(0))
                sMenuRightEntity.SaveChanges()

                If (icount = 1) Then
                    tblMenuRight = New SMENURIGHT
                    tblMenuRight.USERRIGHT = USERRIGHT
                    tblMenuRight.PROGRAMCODE = PROGRAMCODE
                    tblMenuRight.ISSHOW = isShow
                    tblMenuRight.MQUERY = MQUERY
                    tblMenuRight.MADD = MAdd
                    tblMenuRight.MMODIFY = MModify
                    tblMenuRight.MDELETE = MDelete
                    tblMenuRight.MPRINT = MPrint
                    sMenuRightEntity.AddToSMENURIGHT(tblMenuRight)
                End If

            End If
            sMenuRightEntity.SaveChanges()
            result = True
        Catch ex As Exception
            WriteError(ex, "saveData", "menuRight")
            result = False
        End Try

        Return result

    End Function

#End Region


    Protected Sub btnEditMenuRight_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEditMenuRight.Click

        setButton(2)
        bindTogrid()


    End Sub

    Protected Sub btnSaveEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveEdit.Click
      
        sMenuRightEntity = New userEntities
        sMenuRightEntity.Connection.Open()
        Using transaction As System.Data.Common.DbTransaction = sMenuRightEntity.Connection.BeginTransaction()
            Try
                Dim isShow, query, Add, Modify, Delete, Print As Boolean
                Dim chkSelect As CheckBox
                'Dim strUserRight As String = ""
                Dim strProgram As String = ""

                For i As Integer = 0 To gvMenuRight.Rows.Count - 1
                    chkSelect = CType(gvMenuRight.Rows(i).FindControl("chkISSHOW"), CheckBox)
                    isShow = chkSelect.Checked

                    chkSelect = CType(gvMenuRight.Rows(i).FindControl("chkMQuery"), CheckBox)
                    query = chkSelect.Checked

                    chkSelect = CType(gvMenuRight.Rows(i).FindControl("chkMAdd"), CheckBox)
                    Add = chkSelect.Checked

                    chkSelect = CType(gvMenuRight.Rows(i).FindControl("chkModify"), CheckBox)
                    Modify = chkSelect.Checked

                    chkSelect = CType(gvMenuRight.Rows(i).FindControl("chkDelete"), CheckBox)
                    Delete = chkSelect.Checked

                    chkSelect = CType(gvMenuRight.Rows(i).FindControl("chkPrint"), CheckBox)
                    Print = chkSelect.Checked

                    'strUserRight = strUserRight
                    strProgram = gvMenuRight.Rows(i).Cells(0).Text

                    saveData(strProgram, strUserRight, isShow, query, Add, Modify, Delete, Print)
                Next

                transaction.Commit()

                setButton(3)
                ' stuComplete = True
                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('บันทึกเสร็จเรียบร้อยแล้ว')", True)
                IsDataBind()

            Catch ex As Exception
                transaction.Rollback()
                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('Error : " & ex.ToString & " Error message : " & ex.Message & "')", True)

            End Try
        End Using


        sMenuRightEntity = Nothing

    End Sub


    Private Sub setButton(ByVal statusBtn As Integer)
        If (statusBtn = 1) Then 'เริ่มต้น
            btnEditMenuRight.Visible = False
            btnSaveEdit.Visible = False
            btnCancel.Visible = False
            gvMenuRight.Columns(2).Visible = False
            gvMenuRight.Columns(3).Visible = True 'โชว์
            gvMenuRight.Columns(4).Visible = False

            gvMenuRight.Columns(5).Visible = True
            gvMenuRight.Columns(6).Visible = False
            gvMenuRight.Columns(7).Visible = True
            gvMenuRight.Columns(8).Visible = False
            gvMenuRight.Columns(9).Visible = True
            gvMenuRight.Columns(10).Visible = False
            gvMenuRight.Columns(11).Visible = True
            gvMenuRight.Columns(12).Visible = False
            gvMenuRight.Columns(13).Visible = True
            gvMenuRight.Columns(14).Visible = False
      
        ElseIf (statusBtn = 2) Then 'แก้ไข
            btnEditMenuRight.Visible = False
            btnSaveEdit.Visible = True
            btnCancel.Visible = True
            gvMenuRight.Columns(2).Visible = True
            gvMenuRight.Columns(3).Visible = False
            gvMenuRight.Columns(4).Visible = True

            gvMenuRight.Columns(5).Visible = False
            gvMenuRight.Columns(6).Visible = True
            gvMenuRight.Columns(7).Visible = False
            gvMenuRight.Columns(8).Visible = True
            gvMenuRight.Columns(9).Visible = False
            gvMenuRight.Columns(10).Visible = True
            gvMenuRight.Columns(11).Visible = False
            gvMenuRight.Columns(12).Visible = True
            gvMenuRight.Columns(13).Visible = False
            gvMenuRight.Columns(14).Visible = True
        ElseIf (statusBtn = 3 Or statusBtn = 4) Then 'บันทึก ,ยกเลิก
            btnEditMenuRight.Visible = True
            btnSaveEdit.Visible = False
            btnCancel.Visible = False
            gvMenuRight.Columns(2).Visible = False
            gvMenuRight.Columns(3).Visible = True
            gvMenuRight.Columns(4).Visible = False

            gvMenuRight.Columns(5).Visible = True
            gvMenuRight.Columns(6).Visible = False
            gvMenuRight.Columns(7).Visible = True
            gvMenuRight.Columns(8).Visible = False
            gvMenuRight.Columns(9).Visible = True
            gvMenuRight.Columns(10).Visible = False
            gvMenuRight.Columns(11).Visible = True
            gvMenuRight.Columns(12).Visible = False
            gvMenuRight.Columns(13).Visible = True
            gvMenuRight.Columns(14).Visible = False
        End If

    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click

        setButton(4)
        bindTogrid()
    End Sub

    Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        'Dim param As String = "{ 'rptType' : '3'}"
        'ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType, "key", "OpenWindowWithPost('../Form/frmRptMaster.aspx','width=730,height=345,left=100,top=100,resizable=yes,scrollbars=yes','Report', " + param + ");", True)

        Response.Redirect("userRightList.aspx")
    End Sub

    Protected Sub ddlDepartment_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDepartment.SelectedIndexChanged
        Try
            Dim clsDropDown As New clsMasterDropDown
            clsSessionMenuRight = Session("ClsMenuRight")
            If Not (ddlDepartment.SelectedValue = "0") Then

                clsDropDown.setUserRightToDdlSearch(ddlUSerRight, strRoleCode:=clsSessionMenuRight.getRoleGroup, strDepCode:=ddlDepartment.SelectedValue)
            Else
                clsDropDown.setUserRightToDdlSearch(ddlUSerRight, strRoleCode:=clsSessionMenuRight.getRoleGroup, strDepCode:=ddlDepartment.SelectedValue)

                gvMenuRight.DataSource = Nothing
                gvMenuRight.DataBind()
            End If
            clsDropDown = Nothing
        Catch ex As Exception
            WriteError(ex, "ddlDepartment_SelectedIndexChanged", "menuRight")
        End Try


    End Sub

    Protected Sub gvMenuRight_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvMenuRight.SelectedIndexChanged
        Dim chkSelect As CheckBox

        Dim SelectButton As LinkButton = CType(gvMenuRight.Rows(gvMenuRight.SelectedIndex).Cells(2).Controls(0), LinkButton)
        If (SelectButton.Text = "เลือกทั้งหมด") Then
            SelectButton.Text = "ไม่เลือกทั้งหมด"
            chkSelect = CType(gvMenuRight.Rows(gvMenuRight.SelectedIndex).FindControl("chkISSHOW"), CheckBox)
            If Not (chkSelect.Visible = False) Then
                chkSelect.Checked = True
            Else
                chkSelect.Checked = False
            End If

            chkSelect = CType(gvMenuRight.Rows(gvMenuRight.SelectedIndex).FindControl("chkMQuery"), CheckBox)
            If Not (chkSelect.Visible = False) Then
                chkSelect.Checked = True
            Else
                chkSelect.Checked = False
            End If

            chkSelect = CType(gvMenuRight.Rows(gvMenuRight.SelectedIndex).FindControl("chkMAdd"), CheckBox)
            If Not (chkSelect.Visible = False) Then
                chkSelect.Checked = True
            Else
                chkSelect.Checked = False
            End If

            chkSelect = CType(gvMenuRight.Rows(gvMenuRight.SelectedIndex).FindControl("chkModify"), CheckBox)
            If Not (chkSelect.Visible = False) Then
                chkSelect.Checked = True
            Else
                chkSelect.Checked = False
            End If

            chkSelect = CType(gvMenuRight.Rows(gvMenuRight.SelectedIndex).FindControl("chkDelete"), CheckBox)
            If Not (chkSelect.Visible = False) Then
                chkSelect.Checked = True
            Else
                chkSelect.Checked = False
            End If

            chkSelect = CType(gvMenuRight.Rows(gvMenuRight.SelectedIndex).FindControl("chkPrint"), CheckBox)
            If Not (chkSelect.Visible = False) Then
                chkSelect.Checked = True
            Else
                chkSelect.Checked = False
            End If

        ElseIf (SelectButton.Text = "ไม่เลือกทั้งหมด") Then
            SelectButton.Text = "เลือกทั้งหมด"
            chkSelect = CType(gvMenuRight.Rows(gvMenuRight.SelectedIndex).FindControl("chkISSHOW"), CheckBox)
            If Not (chkSelect.Visible = False) Then
                chkSelect.Checked = False
            Else
                chkSelect.Checked = False
            End If

            chkSelect = CType(gvMenuRight.Rows(gvMenuRight.SelectedIndex).FindControl("chkMQuery"), CheckBox)
            If Not (chkSelect.Visible = False) Then
                chkSelect.Checked = False
            Else
                chkSelect.Checked = False
            End If

            chkSelect = CType(gvMenuRight.Rows(gvMenuRight.SelectedIndex).FindControl("chkMAdd"), CheckBox)
            If Not (chkSelect.Visible = False) Then
                chkSelect.Checked = False
            Else
                chkSelect.Checked = False
            End If

            chkSelect = CType(gvMenuRight.Rows(gvMenuRight.SelectedIndex).FindControl("chkModify"), CheckBox)
            If Not (chkSelect.Visible = False) Then
                chkSelect.Checked = False
            Else
                chkSelect.Checked = False
            End If

            chkSelect = CType(gvMenuRight.Rows(gvMenuRight.SelectedIndex).FindControl("chkDelete"), CheckBox)
            If Not (chkSelect.Visible = False) Then
                chkSelect.Checked = False
            Else
                chkSelect.Checked = False
            End If

            chkSelect = CType(gvMenuRight.Rows(gvMenuRight.SelectedIndex).FindControl("chkPrint"), CheckBox)
            If Not (chkSelect.Visible = False) Then
                chkSelect.Checked = False
            Else
                chkSelect.Checked = False
            End If

        End If

        'chkSelect = CType(gvMenuRight.Rows(gvMenuRight.SelectedIndex).FindControl("chkISSHOW"), CheckBox)
        'If Not (chkSelect.Visible = False) Then
        '    chkSelect.Checked = True
        'End If

        'chkSelect = CType(gvMenuRight.Rows(gvMenuRight.SelectedIndex).FindControl("chkMQuery"), CheckBox)
        'If Not (chkSelect.Visible = False) Then
        '    chkSelect.Checked = True
        'End If

        'chkSelect = CType(gvMenuRight.Rows(gvMenuRight.SelectedIndex).FindControl("chkMAdd"), CheckBox)
        'If Not (chkSelect.Visible = False) Then
        '    chkSelect.Checked = True
        'End If

        'chkSelect = CType(gvMenuRight.Rows(gvMenuRight.SelectedIndex).FindControl("chkModify"), CheckBox)
        'If Not (chkSelect.Visible = False) Then
        '    chkSelect.Checked = True
        'End If

        'chkSelect = CType(gvMenuRight.Rows(gvMenuRight.SelectedIndex).FindControl("chkDelete"), CheckBox)
        'If Not (chkSelect.Visible = False) Then
        '    chkSelect.Checked = True
        'End If

        'chkSelect = CType(gvMenuRight.Rows(gvMenuRight.SelectedIndex).FindControl("chkPrint"), CheckBox)
        'If Not (chkSelect.Visible = False) Then
        '    chkSelect.Checked = True
        'End If


        clearGvIndex()
    End Sub

    Private Sub clearGvIndex()
        gvMenuRight.SelectedIndex = -1

    End Sub

    Protected Sub gvMenuRight_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvMenuRight.RowDataBound
       

        If (e.Row.RowState = DataControlRowState.Normal Or e.Row.RowState =
DataControlRowState.Alternate) _
And e.Row.RowType = DataControlRowType.DataRow Then
            Dim chkSelect As CheckBox
            Dim iCount As Integer = 0

            chkSelect = CType(e.Row.FindControl("chkISSHOW"), CheckBox)
            If Not (chkSelect.Visible = False) Then

                If chkSelect.Checked = True Then
                    iCount += 1
                End If
            Else
                iCount += 1
            End If



            chkSelect = CType(e.Row.FindControl("chkMQuery"), CheckBox)
            If Not (chkSelect.Visible = False) Then

                If chkSelect.Checked = True Then
                    iCount += 1
                End If
            Else
                iCount += 1

            End If

            chkSelect = CType(e.Row.FindControl("chkMAdd"), CheckBox)
            If Not (chkSelect.Visible = False) Then

                If chkSelect.Checked = True Then
                    iCount += 1
                End If
            Else
                iCount += 1

            End If

            chkSelect = CType(e.Row.FindControl("chkModify"), CheckBox)
            If Not (chkSelect.Visible = False) Then

                If chkSelect.Checked = True Then
                    iCount += 1
                End If
            Else
                iCount += 1

            End If

            chkSelect = CType(e.Row.FindControl("chkDelete"), CheckBox)
            If Not (chkSelect.Visible = False) Then

                If chkSelect.Checked = True Then
                    iCount += 1
                End If
            Else
                iCount += 1

            End If

            chkSelect = CType(e.Row.FindControl("chkPrint"), CheckBox)
            If Not (chkSelect.Visible = False) Then

                If chkSelect.Checked = True Then
                    iCount += 1
                End If
            Else
                iCount += 1

            End If


            Dim SelectButton As LinkButton = CType(e.Row.Cells(2).Controls(0), LinkButton)
            If (iCount < 6) Then
                SelectButton.Text = "เลือกทั้งหมด"
            Else
                SelectButton.Text = "ไม่เลือกทั้งหมด"
            End If
            'If () Then
            'End If



        End If
    End Sub
End Class
